package com.webframe.web.controller;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.webframe.constant.WFConstant;
import com.webframe.service.def.ITableOperationService;

/**
 * 类功能描述：
 *
 * Description: 
 */
@Controller
@RequestMapping("/tableRowQuery")
public class TableRowQueryController extends BaseController
{
	@Autowired
	private ITableOperationService tableOperationService;

	@SuppressWarnings({"rawtypes", "unchecked"})
	@RequestMapping(method = RequestMethod.POST)
	protected ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String tableName = request.getParameter(WFConstant.Constant_TableName_);
		String id = request.getParameter(WFConstant.Constant_PK_);
		String succesePage = request.getParameter(WFConstant.Constant_Success_PAGE_);
		String errorPage = request.getParameter(WFConstant.Constant_Error_PAGE_);
		String isAjax = request.getParameter(WFConstant.IS_AJAX_);
		String targetPage = succesePage;
		Map result = tableOperationService.doQueryById(id, tableName);
		if("true".equalsIgnoreCase(isAjax))
		{
			StringBuffer sb = new StringBuffer();
			sb.append("{");
			Set keys = result.keySet();
			for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
				String key = iterator.next().toString();
				sb.append("'"+key+"':'"+result.get(key)+"'");
				if(iterator.hasNext())
					sb.append(",");
			}
			sb.append("}");
			outWrite(response, sb.toString());
			return null;
		}
		return new ModelAndView(targetPage,result);
	}
}
